package cn.iocoder.yudao.module.game.controller.admin.rechargerecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.game.controller.admin.rechargerecord.vo.*;
import cn.iocoder.yudao.module.game.dal.dataobject.rechargerecord.RechargeRecordDO;
import cn.iocoder.yudao.module.game.service.rechargerecord.RechargeRecordService;

@Tag(name = "管理后台 - 充值记录")
@RestController
@RequestMapping("/game/recharge-record")
@Validated
public class RechargeRecordController {

    @Resource
    private RechargeRecordService rechargeRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建充值记录")
    @PreAuthorize("@ss.hasPermission('game:recharge-record:create')")
    public CommonResult<RechargeRecordDO> createRechargeRecord(@Valid @RequestBody RechargeRecordSaveReqVO createReqVO) {
        return success(rechargeRecordService.createRechargeRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新充值记录")
    @PreAuthorize("@ss.hasPermission('game:recharge-record:update')")
    public CommonResult<Boolean> updateRechargeRecord(@Valid @RequestBody RechargeRecordSaveReqVO updateReqVO) {
        rechargeRecordService.updateRechargeRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除充值记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('game:recharge-record:delete')")
    public CommonResult<Boolean> deleteRechargeRecord(@RequestParam("id") Long id) {
        rechargeRecordService.deleteRechargeRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得充值记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('game:recharge-record:query')")
    public CommonResult<RechargeRecordRespVO> getRechargeRecord(@RequestParam("id") Long id) {
        RechargeRecordDO rechargeRecord = rechargeRecordService.getRechargeRecord(id);
        return success(BeanUtils.toBean(rechargeRecord, RechargeRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得充值记录分页")
    @PreAuthorize("@ss.hasPermission('game:recharge-record:query')")
    public CommonResult<PageResult<RechargeRecordRespVO>> getRechargeRecordPage(@Valid RechargeRecordPageReqVO pageReqVO) {
        PageResult<RechargeRecordDO> pageResult = rechargeRecordService.getRechargeRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RechargeRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出充值记录 Excel")
    @PreAuthorize("@ss.hasPermission('game:recharge-record:export')")
    @OperateLog(type = EXPORT)
    public void exportRechargeRecordExcel(@Valid RechargeRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RechargeRecordDO> list = rechargeRecordService.getRechargeRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "充值记录.xls", "数据", RechargeRecordRespVO.class,
                        BeanUtils.toBean(list, RechargeRecordRespVO.class));
    }

}